<template>
  <div>
    <div class="home-page" v-if="isHome">
      <search v-bind:searchData="searchData"></search>
      <busPlan ref="updateAddress"></busPlan>
      <usedLine v-bind:usedLineData="usedLineData"></usedLine>
      <!--<div class="data-source-style" v-show="isSource">-->
        <span class="data-source-font" v-show="isSource">数据来源：{{dataSource}}</span>
      <!--</div>-->
    </div>

    <div v-else>
      <div class="limit-style">
        <div class="limit-img-style"><img class="limit-img" src="../../assets/image/limit-state.png" alt=""></div>
      </div>
      <div class="font-style"><span>使用该应用需要您进行登录</span></div>
      <div class="login-style">
        <div class="login-button" v-on:click="login()"><span>立即登录</span></div>
      </div>
    </div>
  </div>

</template>

<script>
  import search from '../common/Search.vue';
  import busPlan from '../common/BusPlan.vue';
  import usedLine from '../common/UsedLine.vue';
  export default {
    data () {
      return {
        searchData: {
          placeholder: '查询线路、公交站点',
          isPullDown: false
        },
        usedLineData: {},
        dataSource: '',
        isHome: true,
        isSource: false
      };
    },
    /*
    打开首页，调用请求，获取数据如下：当前城市、我的当前位置、数据来源、我的常用路线
     */
    created () {
      let _this = this;
      var userData = {};
      var version;
      var selectCityCode; // 用户选择城市
      if (!this.$storage.getStorage('userData')) {
        iCityBridge.init(function (hasLogin) {
          if (hasLogin) {
            iCityBridge.getData({type: 'baseInfo'}, function (resp) {
              var obj = resp;
              userData.mobile = obj.data.phone;
            });
            version = iCityBridge.getVersion();
            selectCityCode = iCityBridge.getCityCode();
            iCityBridge.getData({type: 'location'}, function (resp) {
              var obj = resp;
              if (version === '3.0.0' || version === '3.0.5' || version === '3.1.0') { // code码不统一版本
                if (_this.isAndroid()) {
                  if (obj.data.status === 1) { // 定位成功
                    userData.longitude = obj.data.longitude;
                    userData.latitude = obj.data.latitude;
                  } else {
                    userData.longitude = null;
                    userData.latitude = null;
                    _this.$utils.toastMessage('无法获取当前定位！');
                  }
                }
                if (_this.isIOS()) {
                  if (obj.data.status === 2) { // 定位成功
                    userData.longitude = obj.data.longitude;
                    userData.latitude = obj.data.latitude;
                  } else {
                    userData.longitude = null;
                    userData.latitude = null;
                    _this.$utils.toastMessage('无法获取当前定位！');
                  }
                }
              } else { // 其余版本
                var versionNum = [];
                versionNum = version.split('.');
                if (versionNum[0] < 3) { // 低版本
                  userData.longitude = obj.data.longitude;
                  userData.latitude = obj.data.latitude;
                } else {
                  if (obj.data.status === 1) { // 定位成功
                    userData.longitude = obj.data.longitude;
                    userData.latitude = obj.data.latitude;
                  } else {
                    userData.longitude = null;
                    userData.latitude = null;
                    _this.$utils.toastMessage('无法获取当前定位！');
                  }
                }
              }
              userData.selectCityCode = selectCityCode;
              _this.$storage.setStorage('userData', userData);
              _this.isHome = true;
              _this.initHomeData();
            });
          } else {
            _this.isHome = false;
          }
          // 分享
          iCityBridge.hideLoading();
        });
      }
      // _this.initHomeData();
      // _this.isHome = true;
      if (this.$storage.getStorage('usedLineData')) {
        this.usedLineData = this.$storage.getStorage('usedLineData');
      }
      if (this.$storage.getStorage('dataSource')) {
        this.isSource = true;
        this.dataSource = this.$storage.getStorage('dataSource');
      }
      if (this.$storage.setStorage('intervalID')) {
        clearInterval(this.$storage.setStorage('intervalID'));
      }
    },
    methods: {
      initHomeData: function () {
        var param = this.$storage.getStorage('userData');
        // var param = {};
        // param.longitude = '114.465919';
        // param.latitude = '38.066145';
        // param.longitude = null;
        // param.latitude = null;
        // param.mobile = '18753120351';
        // param.selectCityCode = '130100';
        // this.$storage.setStorage('userData', param);
        var _this = this;
        this.$utils.indicatorOpen();
        this.$baseService.getData(_this.$global.API_GET_HOME_DADA, param, function (data) {
          _this.usedLineData = {};
          _this.usedLineData = Object.assign({}, _this.usedLineData, data.usedRouteData);
          _this.$storage.setStorage('usedLineData', _this.usedLineData); // 常用线路
          if (data.currentAddress) {
            let currentAddress = {};
            currentAddress.address = data.currentAddress;
            currentAddress.lng = param.longitude;
            currentAddress.lat = param.latitude;
            currentAddress.type = 0; // 地点
            currentAddress.city = data.positionCity;
            _this.$storage.setStorage('currentAddress', currentAddress); // 当前位置
          }
          if (data.positionCity) {
            _this.$storage.setStorage('positionCity', data.positionCity); // GPS定位城市
          }
          if (data.currentCity) {
            _this.$storage.setStorage('currentCity', data.currentCity); // 当前城市
          }
          if (data.areacode) {
            _this.$storage.setStorage('areacode', data.areacode); // 系统代码
          }
          if (data.dataSource) {
            _this.isSource = true;
            _this.dataSource = data.dataSource;
            _this.$storage.setStorage('dataSource', data.dataSource); // 数据来源
          }
          /*
          调用BusPlan组件方法，更新我的位置
           */
          _this.$refs.updateAddress.updateCurrentAddress();
        });
      },
      login: function () {
        iCityBridge.login();
      },
      isAndroid: function () {
        var ua = navigator.userAgent;
        return ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1;
      },
      isIOS: function () {
        var ua = navigator.userAgent;
        return !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      }
    },
    components: {
      search,
      busPlan,
      usedLine
    }
  };
</script>

<style>
  .home-page {
    margin-top: 0.26rem;
  }

  .data-source-font {
    position:absolute;
    bottom:0.8rem;
    left: 20%;
    width: 60%;
    color: #999999;
    font-size: 0.32rem;
    text-align: center;
  }

  .limit-style{
    display: flex;
    width: 100%;
  }

  .limit-img-style{
    margin:0 auto;
    margin-top: 1.78rem;
  }

  .limit-img{
    width: 5.46rem;
    height: 4.53rem;

  }
  .font-style{
    margin-top: 0.26rem;
    text-align: center;
    color: #999999;
    font-size: 0.4rem;
  }

  .login-style{
    margin-top: 1.41rem;
  }

  .login-button {
    background-color: #FF952D;
    width: 7.73rem;
    height: 1.2rem;
    margin: 0px auto;
    border-radius: 4px;
    text-align: center;
    display: flex;

  }

  .login-button span {
    color: #ffffff;
    align-self: center;
    flex: 1;
    font-size: 0.4rem;
  }
</style>
